package com.gopay.scheduler.job;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gopay.common.constants.trans.TxnStaCode;
import com.gopay.common.cps.dao.order.CpsGeneralMainOrderQueryDAO;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.order.MainOrderField;
import com.gopay.remote.order.MainOrderUpdateRemoteService;
import com.gopay.scheduler.GopayBaseJob;


/**
 * 超时订单清理任务。
 * 
 * 在此任务中，主要进行以下工作：
 * 1.其他订单：根据超时时间清理除银行订单以外的超时订单，订单状态为处理中并且订单的创建时间与当前时间的时间差大于等于timeSpan
 * ，订单交易状态设置为“失效” 2.银行订单：根据对账次数清理银行订单中的超时订单，订单状态为处理中并且订单的对账次数大于等于
 * totalCheckTimes ，订单交易状态设置为“失效”
 * 
 * @author 王丽
 */
public class CheckTimeoutOrderJob extends GopayBaseJob {

	/* IOC注入方法开始 */
	private CpsGeneralMainOrderQueryDAO cpsGeneralMainOrderDAO;
	private MainOrderUpdateRemoteService cpsGeneralMainOrderOperClient;

	@Resource(name = "cpsGeneralMainOrderOperClient")
	public void setCpsGeneralMainOrderOperClient(
			MainOrderUpdateRemoteService cpsGeneralMainOrderOperClient) {
		this.cpsGeneralMainOrderOperClient = cpsGeneralMainOrderOperClient;
	}

	@Resource(name = "cpsGeneralMainOrderDAO")
	public void setCpsGeneralMainOrderDAO(
			CpsGeneralMainOrderQueryDAO cpsGeneralMainOrderDAO) {
		this.cpsGeneralMainOrderDAO = cpsGeneralMainOrderDAO;
	}
	private Logger logger = LoggerFactory
			.getLogger(CheckTimeoutOrderJob.class);
	public void doJobInternal() {

//		Logger logger = LoggerFactory.getLogger(CheckTimeoutOrderJob.class);
//
//		Date now = new Date();
//		MDC.put("jobId", "CheckTimeoutOrderJob-" + (now.getYear() + 1900) + "-"
//				+ now.getMonth() + "-" + now.getDate() + "-" + now.getHours()
//				+ "-" + now.getMinutes() + "-" + now.getSeconds());
		String txnStaCode = String.valueOf(TxnStaCode.TXN_STA_CD_20002.value);

		// 根据超时时间清理除银行订单以外的超时订单
		// 超时时间（分钟）
		int timeSpan = 45;
		List<Object> otherMainOrederList = cpsGeneralMainOrderDAO
				.findTimeoutCpsGenMainOrderByTimeSpan(timeSpan);
		if (!(otherMainOrederList.isEmpty())) {

			logger.info("found" + otherMainOrederList.size()
					+ "other orders (except the bank order) records, overtime timeout order clean-up work now!");
			Map<MainOrderField, String> map = new HashMap<MainOrderField, String>();
			
			map.put(MainOrderField.TXN_STA_CD, txnStaCode);
			SimpleDateFormat sf =new SimpleDateFormat("yyyyMMddHHmmss");
			map.put(MainOrderField.TRAN_FINISH_TIME, sf.format(new Date()));
			for (Object obj : otherMainOrederList) {

				String gopayOrderId = String.valueOf(obj);

				try {
					
					cpsGeneralMainOrderOperClient.updateMany(gopayOrderId,
							map);
				} catch (GopayException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();

				}
			}
			logger.info("task - timeout the order has been done to clean up regularly");
		} else {
			
			logger.info("found" + otherMainOrederList.size()
					+ "Other orders (except the bank order) overtime record, not for processing");
		}

	}

}
